package com.ccp.dev.form.service;

import com.ccp.dev.core.basic.base.BaseService;
import com.ccp.dev.core.basic.util.UUIDUtils;
import com.ccp.dev.core.util.BeanUtils;
import com.ccp.dev.form.dao.FormRuleDao;
import com.ccp.dev.form.model.FormRule;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * 表单规则验证 service类
 *
 * @author chenjing
 */
@Service
public class FormRuleService extends BaseService<FormRule> {

    @Resource
    private FormRuleDao formRuleDao;

    /**
     * 根据 id 批量删除数据
     * @param ids ids
     */
    @Transactional(rollbackFor = Exception.class)
    public void delByIds(String[] ids){
        for(String id : ids){
            formRuleDao.delById(id);
        }
    }

    /**
     * 保存表单验证规则
     *
     * @param formRule formRule
     */
    @Transactional(rollbackFor = Exception.class)
    public void save(FormRule formRule) {
        if (BeanUtils.isNotEmpty(formRule.getId())) {
            formRuleDao.update(formRule);
        } else {
            formRule.setId(UUIDUtils.getUUIDFor32());
            formRule.setCreateTime(new Date());
            formRuleDao.add(formRule);
        }
    }

    /**
     * 查询别名是否已存在
     * @param alias
     * @return
     */
    public boolean queryForAlias(String alias){
        return formRuleDao.getByAlias(alias) != null;
    }

    /**
     * 根据别名集合查找规则数据集合
     * @param setAlias 别名集合
     * @return 规则集合
     */
    public List<FormRule> getRulesBySetAlias(Set<String> setAlias){
        if(setAlias.size() == 0){
            return null;
        }
        return formRuleDao.getRulesBySetAlias(setAlias);
    }
}